package com.pipi.sso.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.pipi.sso.core.enums.CommonStatusEnum;
import com.pipi.sso.core.enums.DormErrorCodeEnum;
import com.pipi.sso.core.exception.DormServiceException;
import com.pipi.sso.core.model.CollegeBO;
import com.pipi.sso.core.service.CollegeService;
import com.pipi.sso.core.util.BeanUtils;
import com.pipi.sso.dao.manager.CollegeManager;
import com.pipi.sso.dao.model.CollegeDO;
import com.pipi.sso.dao.query.CollegeQuery;
import com.pipi.sso.dao.support.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CollegeServiceImpl implements CollegeService {

    @Resource
    private CollegeManager collegeManager;

    @Override
    public PageResult<CollegeBO> queryCollegeByPage(CollegeBO collegeBO) {
        try {
            CollegeQuery query = buildCollegeQuery(collegeBO);
            query.setPageNo(collegeBO.getPage());
            query.setPageSize(collegeBO.getLimit());
            PageResult<CollegeDO> collegeDOS = collegeManager.selectByQueryWithPage(query);
            if (CollectionUtils.isEmpty(collegeDOS.getResult())) {
                return PageResult.createPageResult();
            }
            List<CollegeBO> collegeBOS = collegeDOS.getResult().stream().map(collegeDO -> BeanUtils.createCloneBean(collegeDO, CollegeBO.class)).collect(Collectors.toList());
            return PageResult.createPageResult(collegeBOS, collegeDOS.getPageInfo());
        } catch (DormServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error(DormErrorCodeEnum.QUERY_COLLEGE_FAIL.getErrMsg() + "collegeBO is {}", JSON.toJSONString(collegeBO), e);
            throw new DormServiceException(DormErrorCodeEnum.QUERY_COLLEGE_FAIL);
        }
    }

    @Override
    public List<CollegeBO> queryCollege(CollegeBO collegeBO) {
        try {
            CollegeQuery query = buildCollegeQuery(collegeBO);
            List<CollegeDO> collegeDOS = collegeManager.selectByQuery(query);
            if (CollectionUtils.isEmpty(collegeDOS)) {
                return Lists.newArrayList();
            }
            return collegeDOS.stream().map(collegeDO -> BeanUtils.createCloneBean(collegeDO, CollegeBO.class)).collect(Collectors.toList());
        } catch (DormServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error(DormErrorCodeEnum.QUERY_COLLEGE_FAIL.getErrMsg() + "collegeBO is {}", JSON.toJSONString(collegeBO), e);
            throw new DormServiceException(DormErrorCodeEnum.QUERY_COLLEGE_FAIL);
        }
    }

    @Override
    public Boolean addCollege(CollegeBO collegeBO) {
        try {
            CollegeQuery query = buildCollegeQuery(collegeBO);
            List<CollegeDO> collegeDOS = collegeManager.selectByQuery(query);
            if (CollectionUtils.isNotEmpty(collegeDOS)) {
                throw new DormServiceException(DormErrorCodeEnum.PARAM_ERROR.getErrCode(),
                        "该学院已存在");
            }
            CollegeDO collegeDO = new CollegeDO();
            collegeDO.setCollegeName(collegeBO.getCollegeName());
            collegeDO.setStatus(CommonStatusEnum.YES.getCode());
            collegeManager.insertSelective(collegeDO);
            return true;
        } catch (DormServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error(DormErrorCodeEnum.ADD_COLLEGE_FAIL.getErrMsg() +
                    "collegeBO is {}", JSON.toJSONString(collegeBO), e);
            throw new DormServiceException(DormErrorCodeEnum.ADD_COLLEGE_FAIL);
        }
    }

    @Override
    public Boolean updateStatus(CollegeBO collegeBO) {
        try {
            CollegeDO collegeDO = collegeManager.selectByPrimaryKey(collegeBO.getCollegeId());
            if (collegeBO.getStatus().equals(collegeDO.getStatus())) {
                log.error("{}, collegeBo is{}, collegeDO is {}", DormErrorCodeEnum.COLLEGE_STATUS_EXCEPTION, JSON.toJSONString(collegeBO), JSON.toJSONString(collegeDO));
                throw new DormServiceException(DormErrorCodeEnum.COLLEGE_STATUS_EXCEPTION);
            }
            CollegeDO updateCollegeDO = BeanUtils.createCloneBean(collegeBO, CollegeDO.class);
            collegeManager.updateByPrimaryKeySelective(updateCollegeDO);
            return true;
        } catch (DormServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error(DormErrorCodeEnum.COLLEGE_STATUS_EXCEPTION.getErrMsg() + "collegeBO is {}", JSON.toJSONString(collegeBO), e);
            throw new DormServiceException(DormErrorCodeEnum.COLLEGE_STATUS_EXCEPTION);
        }
    }

    private CollegeQuery buildCollegeQuery(CollegeBO collegeBO) {
        CollegeQuery query = new CollegeQuery();
        query.setOrderByClause("gmt_create desc");
        CollegeQuery.Criteria criteria = query.createCriteria().andIsDeletedEqualTo(CommonStatusEnum.NO.getCode());

        if (null != collegeBO) {
            if (null != collegeBO.getCollegeId()) {
                criteria.andCollegeIdEqualTo(collegeBO.getCollegeId());
            }
            if (StringUtils.isNotBlank(collegeBO.getCollegeName())) {
                criteria.andCollegeNameLike("%" + collegeBO.getCollegeName() + "%");
            }
        }
        return query;
    }
}
